package org.example.dao;

import org.example.DBConnection;
import org.example.entity.User;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author xiaomengzi
 * @create 2025-08-04 下午2:51
 */
public class UserDAO {
    private Connection connection;
    public UserDAO() throws SQLException{
        this.connection = DBConnection.getConnection();
    }
    //密码加密算法
    private String hashPassword(String password){
        try{
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] hash = md.digest(password.getBytes());
            StringBuffer sb = new StringBuffer();
            for (byte b : hash) {
                sb.append(String.format("%02x", b));

            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e){
            throw new RuntimeException("密码加密失败",e);
        }
    }
    //创建用户
    public int createUser(User user) throws SQLException{
        String sql = "INSERT INTO ousers (username, password, email, phone, address) VALUES (?, ?, ?, ?, ?)";
        try(PreparedStatement ps = connection.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS)){
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getEmail());
            ps.setString(4,user.getPhone());
            ps.setString(5,user.getAddress());
            ps.executeUpdate();
            try (ResultSet rs = ps.getGeneratedKeys()) {
                if(rs.next()){
                    return rs.getInt(1);
                }
            }
        }
        return -1;
    }
    //通过id获取用户
    public User getUser(int id) throws SQLException{
        String sql = "select * from ousers where id=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setInt(1,id);
            try(ResultSet rs = ps.executeQuery()){
                if(rs.next()){
                    User user = new User();
                    user.setId(rs.getInt(1));
                    user.setUsername(rs.getString(2));
                    user.setPassword(rs.getString(3));
                    user.setEmail(rs.getString(4));
                    user.setPhone(rs.getString(5));
                    user.setAddress(rs.getString(6));
                    user.setCreateDate(rs.getDate(7).toLocalDate());
                    user.setUpdateDate(rs.getDate(8).toLocalDate());
                    return user;
                }
            }
        }
        return null;
    }
    //用户认证
    public User authenticate(String username, String password) throws SQLException{
        String sql = "select * from ousers where username=? and password=?";
        String hashPassword = hashPassword(password);
        try(PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, hashPassword);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt(1));
                    user.setUsername(rs.getString(2));
                    user.setPassword(rs.getString(3));
                    user.setEmail(rs.getString(4));
                    user.setPhone(rs.getString(5));
                    user.setAddress(rs.getString(6));
                    user.setCreateDate(rs.getDate(7).toLocalDate());
                    user.setUpdateDate(rs.getDate(8).toLocalDate());
                    return user;
                }
            }
        }
        return null;
    }
}
